package yxy.game.pm2.active.command.holiday;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actturntabledate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_节日活动;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.List;

/**
 * Author HYB
 * 转盘抽奖
 */
@MessageType(proto = Command.ActHolidayLotteryTurntableRequest, classtype = ActiveMessage.ActHolidayLotteryTurntableRequest.class)
public class CMD_Active_节日_转盘抽奖 extends Command<ActiveMessage.ActHolidayLotteryTurntableRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActHolidayLotteryTurntableRequest request) throws Exception {
        String pcode = messageSource.getTag();
        ActiveMessage.ActHolidayLotteryTurntableResponse.Builder response = ActiveMessage.ActHolidayLotteryTurntableResponse.newBuilder();
        int lotteryNum = request.getType();
        ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }
        if (!(lotteryNum == 1 || lotteryNum == 10)) {
            send(messageSource, response, Result.ParamError);
            return;
        }
        //扣道具
        actturntabledate data = DataCenter.getData(103, actturntabledate.class);
        if (data == null) {
            send(messageSource, response, Result.ParamError);
            return;
        }
        Currency costcur = Currency.create(data.getCosttype1(), data.getCostid1(), data.getCostnum1() * lotteryNum);
        try {
            new CurrencyHandle().cost(pcode, CCReason.活动_节日_转盘, costcur);
        } catch (CurrencyNotEnoughException e) {
            send(messageSource, response, new Result(Result.CostFail, costcur.getType().getNumber(), costcur.getDataId()));
            return;
        }
        am.addLotteryCount(lotteryNum);
        PlayerBean pb = PlayerBean.get(pcode);
        ServerBean sb = ServerBean.get(pb.getServerId());
        List<Currency> cur = am.randomcur(sb.getAreaId(), pb.getName(), lotteryNum);
        new CurrencyHandle().addCurrency(pcode, CCReason.活动_节日_转盘, cur);
        response.addAllDisplay(new CurrencyHandle().packItems(cur));
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActHolidayLotteryTurntableResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
